package be.teletask.onvif.parsers;

import be.teletask.onvif.models.GetAudioOutputConfiguration;
import be.teletask.onvif.models.OnvifProfile;
import be.teletask.onvif.responses.OnvifResponse;
import be.teletask.onvif.util.XmlToJsonUtils;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by Tomas Verhelst on 04/09/2018.
 * Copyright (c) 2018 TELETASK BVBA. All rights reserved.
 */
public class GetAudioOutputConfigurationParser extends OnvifParser<GetAudioOutputConfiguration> {

    //Constants
    public static final String TAG = GetAudioOutputConfigurationParser.class.getSimpleName();

    @Override
    public GetAudioOutputConfiguration parse(OnvifResponse response) {
        JSONObject bodyObjet = XmlToJsonUtils.xmlToJson(response.getXml());
        GetAudioOutputConfiguration audioOutput = new GetAudioOutputConfiguration();

        JSONObject configurationObject = bodyObjet.getJSONObject("GetAudioOutputConfigurationResponse").getJSONObject("Configuration");

        String token = configurationObject.getString("@token");
        String name = configurationObject.getString("Name");
        String outputToken = configurationObject.getString("OutputToken");
        String sendPrimacy = configurationObject.getString("SendPrimacy");
        Integer useCount = 0;
        if(configurationObject.containsKey("UseCount")) {
            useCount = configurationObject.getInteger("UseCount");
        }
        Integer outputLevel = configurationObject.getInteger("OutputLevel");

        audioOutput.setToken(token);
        audioOutput.setName(name);
        audioOutput.setOutputToken(outputToken);
        audioOutput.setSendPrimacy(sendPrimacy);
        audioOutput.setUseCount(useCount);
        audioOutput.setOutputLevel(outputLevel);

        return audioOutput;
    }

}
